package com.yiqi.lottery.feature.football.adapter;


import android.content.Context;
import android.content.res.Resources;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.BallMatchBo;

public class FootballSFCSelectedAdapter extends BaseQuickAdapter<BallMatchBo.Match, BaseViewHolder> {

    private Context mContext;
    private OnMatchSelectListener mListener;
    private BallMatchBo mMatchBo;
    private int mCurrentBetType;

    public FootballSFCSelectedAdapter(Context context, int currentBetType) {
        super(R.layout.item_football_sfc_selected_child);
        mContext = context;
        mCurrentBetType = currentBetType;
    }

    public void setMatchBo(BallMatchBo matchBo) {
        mMatchBo = matchBo;
    }

    public BallMatchBo getMatchBo() {
        return mMatchBo;
    }

    @Override
    protected void convert(BaseViewHolder helper, BallMatchBo.Match item) {
        ((TextView) helper.getView(R.id.tv1_1)).setText(String.valueOf(helper.getAdapterPosition() + 1));
        helper.setText(R.id.tv1_1, String.valueOf(helper.getAdapterPosition() + 1));
        resetAdapterView(item, helper.getView(R.id.ll_parent), mMatchBo.getMatch().size() > (mCurrentBetType == 9 ? 14 : 9));
    }

    private void resetAdapterView(BallMatchBo.Match matchInfo, LinearLayout ll_parent, boolean isNeedDan) {
        int childCount = ll_parent.getChildCount();
        LinearLayout itemView;
        Resources resources = mContext.getResources();

        TextView tvDan = ll_parent.findViewById(R.id.tv5_1);
        View line = ll_parent.findViewById(R.id.line);
        tvDan.setTag(matchInfo);
        if (isNeedDan) {
            tvDan.setVisibility(View.VISIBLE);
            line.setVisibility(View.VISIBLE);
            if (matchInfo.isDan()) {
                tvDan.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                tvDan.setTextColor(resources.getColor(R.color.primary_comparison));
            } else {
                tvDan.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                tvDan.setTextColor(resources.getColor(R.color.text_primary));
            }
            tvDan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    BallMatchBo.Match matchInfo = (BallMatchBo.Match) view.getTag();
                    matchInfo.setDan(!matchInfo.isDan());
                    if (matchInfo.isDan()) {
                        view.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                        ((TextView) view).setTextColor(resources.getColor(R.color.primary_comparison));
                    } else {
                        view.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                        ((TextView) view).setTextColor(resources.getColor(R.color.text_primary));
                    }
                }
            });
        } else {
            tvDan.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        }

        if (matchInfo.getIsCheck() == null) {
            boolean[] isCheck = new boolean[3];
            matchInfo.setIsCheck(isCheck);
        }

        String[] list;
        if (matchInfo.getReserved1().equals("--")) {
            list = null;
        } else {
            list = matchInfo.getReserved1().split("-");
        }
        for (int i = 1; i < childCount; ++i) {
            if (!(ll_parent.getChildAt(i) instanceof LinearLayout)) {
                continue;
            }
            itemView = (LinearLayout) ll_parent.getChildAt(i);
            int index = i / 2;
            itemView.setTag(index);
            itemView.setTag(R.id.tag, matchInfo);
            if (index == 0) {
                ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getHostName()));
                ((TextView) itemView.getChildAt(1)).setText("主胜" + (list == null ? " --":list[index]));
            } else if (index == 1) {
                ((TextView) itemView.getChildAt(0)).setText("VS");
                ((TextView) itemView.getChildAt(1)).setText("平" + (list == null ? " --":list[index]));
            } else if (index == 2) {
                ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getGuestName()));
                ((TextView) itemView.getChildAt(1)).setText("客胜" + (list == null ? " --":list[index]));
            }
            if (matchInfo.isCheck(index)) {
                itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
            } else {
                itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
            }
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int index = (int) view.getTag();
                    BallMatchBo.Match matchInfo = (BallMatchBo.Match) view.getTag(R.id.tag);
                    matchInfo.setCheck(index, !matchInfo.isCheck(index));
                    LinearLayout itemView = (LinearLayout) view;
                    if (matchInfo.isCheck(index)) {
                        itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                        ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                        ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
                    } else {
                        itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                        ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                        ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
                    }
                    if (mListener != null) {
                        mListener.reset();
                    }
                }
            });
        }
        if (mListener != null) {
            mListener.reset();
        }
    }

    public void resetCheck() {
        if (getData() == null || getData().isEmpty()) {
            return;
        }
        for (BallMatchBo.Match match : getData()) {
            match.resetChecks();
        }
        notifyDataSetChanged();
    }

    public int getCheckNum() {
        if (getData() == null || getData().isEmpty()) {
            return 0;
        }
        int checkNum = 0;
        for (BallMatchBo.Match match : getData()) {
            checkNum += (match.isHadChecked() ? 1 : 0);
        }
        return checkNum;
    }


    public void setOnMatchSelectListener(OnMatchSelectListener listener) {
        mListener = listener;
    }

    public interface OnMatchSelectListener {
        public void reset();
    }
}
